package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.ExperimentStepTextMapper;
import com.brillilab.domain.po.experiments.ExperimentStepText;
import com.brillilab.service.core.experiments.IExperimentStepTextService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ExperimentStepTextServiceImpl implements IExperimentStepTextService {

    @Resource
    private ExperimentStepTextMapper experimentStepTextMapper;

    @Override
    public boolean insert(ExperimentStepText experimentStepText) {
        return experimentStepTextMapper.insert(experimentStepText) > 0;
    }

    @Override
    public List<ExperimentStepText> selectList(Long experimentId,Long phaseId,Long stepId) {
        LambdaQueryWrapper<ExperimentStepText> wrapper=new LambdaQueryWrapper<ExperimentStepText>()
                .eq(ExperimentStepText::getExperimentId,experimentId);
//        if(phaseId!=null){
//            wrapper.in(ExperimentStepText::getPhaseId,Arrays.asList(phaseId,0));
//        }
//        if(stepId!=null){
//            wrapper.in(ExperimentStepText::getStepId,Arrays.asList(stepId,0));
//        }
        return experimentStepTextMapper.selectList(wrapper);
    }
    
    @Override
    public List<ExperimentStepText> selectList(Long experimentId) {
        LambdaQueryWrapper<ExperimentStepText> wrapper=new LambdaQueryWrapper<ExperimentStepText>()
                .eq(ExperimentStepText::getExperimentId,experimentId);
        return experimentStepTextMapper.selectList(wrapper);
    }

    @Override
    public ExperimentStepText selectById(Long stepTextId) {
        return experimentStepTextMapper.selectById(stepTextId);
    }

    @Override
    public boolean deleteById(Long textId) {
        return experimentStepTextMapper.deleteById(textId) >=0 ;
    }

    @Override
    public boolean deleteByExperimentId(Long experimentId) {
        return experimentStepTextMapper.delete(
                new LambdaQueryWrapper<ExperimentStepText>()
                        .eq(ExperimentStepText::getExperimentId,experimentId))>0;
    }

    @Override
    public int insertBatch(List<ExperimentStepText> texts){
        if(CollectionUtils.isNotEmpty(texts)){
            texts = texts.stream().peek(e -> {
                if(e.getStepId() == null || e.getPhaseId() == null) e.setStepId(0L) ; e.setPhaseId(0L);
            }).collect(Collectors.toList());
            return experimentStepTextMapper.insertBatch(texts);
        }
        return 0;
    }

    @Override
    public boolean deleteByExperimentIdAndType(Long experimentId, Integer type) {
        return experimentStepTextMapper.delete(
                new LambdaQueryWrapper<ExperimentStepText>()
                        .eq(ExperimentStepText::getExperimentId, experimentId)
                        .eq(ExperimentStepText::getType, type)) > 0;
    }
}
